package cn.linstudy.controller;

import cn.linstudy.domain.Department;
import cn.linstudy.qo.DepartmentQuertObject;
import cn.linstudy.qo.PageResult;
import cn.linstudy.result.ResponseResult;
import cn.linstudy.service.DepartmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


/**
 * @Description
 * @Author XiaoLin
 * @Date 2021/3/2 17:17
 */
@Controller
@Slf4j
@RequestMapping("/dept")
public class DepartmentController {



  @Autowired
  DepartmentService departmentService;

  @RequestMapping("listPage")
  public String ListPageDepartment(Model model, DepartmentQuertObject qo){
    PageResult<Department> departmentPageResult = departmentService.listForPage(qo);
    model.addAttribute("departmentPageResult",departmentPageResult);
    return "department/list";
  }

  @RequestMapping("input")
  public String toInput(){
    return "department/input";
  }

  @RequestMapping("saveOrUpdate")
  public String saveOrUpdate(Long id,Department department,Model model){
    if (id == null){
      departmentService.add(department);
      return "redirect:/dept/listPage";
    }else{
      ResponseResult responseResult = departmentService.listOneById(department.getId());
      model.addAttribute("department",responseResult.getData());
      return "department/input";
    }

  }

  @RequestMapping("update")
  public String update(Department department){
    departmentService.update(department);
    return "redirect:/dept/listPage";
  }

  @RequestMapping("delete")
  public String delete(Long id){
    departmentService.delete(id);
    return "redirect:/dept/listPage";
  }



}
